<?php
 session_start();
 include 'db.php';
 include 'input.php';

 $biaoti = trim($input->post('biaoti'));
 $content = trim($input->post('content'));

  $biao = htmlspecialchars($biaoti);
   
 $time = time();
 if($biaoti!=false && $content!=false){
  
     $sql = "insert into news(biao,content,shijian) values('{$biao}','{$content}','{$time}')";

     echo $sql;
     
     if($conn->query($sql)){
         
         header("Location:index.php");
     }
     
 }
 
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>列表信息</title>
<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
  <script type="text/javascript">
     $("#btn").click(function(){
		var f = document.form[0];
		f.submit();
     })
  </script>
</head>
<body>
<?php
if($_SESSION['username']){
    echo '欢迎：'.$_SESSION['username'];
}
?>



<form action="index.php" method="post">
标题：<input type="text" name="biaoti"><br/>
内容：<textarea rows="5px" cols="20px" name="content"> 
</textarea><br/>
<button id="btn">保存</button>
</form>
  <?php
    
    $pageNo = $input->get('pageNo');
    if($pageNo == false){
        $pageNo = 1;
    }
    $pageN = ($pageNo-1)*3;
    $sql = "select * from news where 1=1";
    if($biaoti!=false){
       $sql .= " and biaoti like '%{$biaoti}%'"; 
    }
    
    if($content!=false){
        $sql .= " and content like '%{$content}%'";
    }
     
     $sql .= " ORDER BY id desc ";
     $sql .= "limit {$pageN},3";
     
     $result = $conn->query($sql);
     while($row = $result->fetch_array(MYSQLI_ASSOC)){
          $rows[] = $row;
          
      }
  
?>

   <div>
   <form action="index.php" method="post">
   标题:<input type="text" name="biaoti"> 内容：<input type="text" name="content">
   <input type="submit" value="搜索">
   </form>
   <table border="1px solid">
     <tr style="text-align:center">
     	<td>标题</td>
     	<td>内容</td>
     	<td>时间</td>
     	<td>操作</td>
     </tr>
     
  <?php 
  foreach($rows as $r){
    $t = date('Y-m-d H:i:s',$r['shijian']);
  ?>
 
  
     <tr>
     	<td><?php echo $r['biaoti']?></td>
     	<td><?php echo $r['content']?></td>
     	<td><?php echo $t?></td>
     	<td>
     	<?php 
     	 if(isset($_SESSION['username'])){
     	 ?>
          <button onclick="del(<?php echo $r['id']?>)">删除</button>
          <a href="update.php?do=<?php echo $r['id']?>">修改</a>
          <?php }else{
              echo "无权限";
          }?>
		</td>
     </tr>
     <?php 
}
   ?>
      </table>
      <a href="index.php?pageNo=1">首页</a>
     <?php 
      if($pageNo <= 1){
          echo "上一页";
      }else{
     ?>
      <a href="index.php?pageNo=<?php echo $pageNo-1?>">上一页</a>
      <?php }?>
      
      <?php 
        if($pageNo >= $pageSize){
            echo "下一页";
        }else{
      ?>
      <a href="index.php?pageNo=<?php echo $pageNo+1?>">下一页</a>
      <?php }?>
      <a href="index.php?pageNo=<?php echo $pageSize?>">尾页</a>
      
      共：<?php echo $pageSize?>页|当前是：<?php echo $pageNo?>页
      
      跳转：<input type="text" onblur="show(this.value)">页
   </div>
   <form action="" name="f">
    <input type="hidden" name="do">
   </form>
<script type="text/javascript">

	function show(a){
          
		window.location.href = "index.php?pageNo="+a;
		
	}

  function del(val){
	
      if(window.confirm("确认删除吗")){
          
              var f = document.forms['f'];
              f.action = 'delete.php';
              f.method = 'get';
              f.do.value = val;
              f.submit();
      }
      
  }
</script>
</body>
</html>